<?php

	session_start();
	/*
	Info: Adds new entry line to a story
	Author: Pritam Pebam
	Version: 1.0
	Contact: ptamzz@gmail.com; @ptamzz
	URL: http://12bubbles.com
	*/
	
	require_once('../inc/fnc.php');
	require_once('../inc/conn.php');
	require_once('../inc/settings.php');
	
	if(isset($_POST['newline'])){
		// Initializing variables
		//$line = mysql_prep(rawurldecode($_POST['newline']));
		$line = mysql_prep(htmlentities($_POST['newline']));
		$lineId = mysql_prep($_POST['lineId']);
		$storyId = mysql_prep($_POST['storyId']);
		$userId = $_SESSION['userId'];
		if(is_logged_in()) {
			$time = time();
			
			$timer = 0;
			$countFlag = 0;
			$lineId = getnewentryid($storyId);
			$slid = getnewstorylineid($storyId);

			$query = "INSERT INTO entryline (`storyid`,`storylineid`,`entrylineid`,`entryline`,`crtdbyid`,`delflag`,`crtdtime`) values ('{$storyId}','{$slid}','{$lineId}','{$line}','{$userId}','N',NOW())";
			mysql_query($query, $connection);
			
			$query = mysql_query("select displayseq,entryline from entryline WHERE `storyid` = '{$storyId}' and `storylineid` = '{$slid}' and `entrylineid` = '{$lineId}'");
			while($row = mysql_fetch_array($query)) {
				$disseq = $row['displayseq'];
				$line = $row['entryline'];
			}
			addtonotification($storyId,$slid,$disseq,3);
			$updateNode = "newline" . $lineId;
			$delNode = "line" . $lineId;
			$args = array (
						'count' => $lineId,
						'lineId' => $lineId,
						'line' => $line,
						'voteUp' => 0,
						'voteDwn' => 0,
						'updateNode' => $updateNode,
						'upClass' => 'voteUp',
						'dwnClass' => 'voteDwn',
						'delNode' => $delNode,
						'deleter' => $_SESSION['deleter'],
						'user' => $userId,
						'level' => 2,
						'uniqueid'  => $disseq,
						'crtdbyid' => $userId,
					);
						
			// Printing out each line
			$_SESSION['time'] = time();
			$return['type'] = 'registered';
			$return['line'] = getEntryLine($args);
		} else {
			unset($_SESSION['indicator']);
			unset($_SESSION['redirecturl']);
			$_SESSION['redirecturl'] = $_SERVER['HTTP_REFERER'];
			$_SESSION['sline'] = $line;
			$_SESSION['indicator'] = '10';
			$return['type'] = 'visitor';
		}
		echo json_encode($return);
	}
	 
	
?>
